package com.itheima.topic2;

import org.junit.Test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

public class Mytest {
    /**1. 现有集合：ArrayList\<Integer>list = new ArrayList();
    2. 利用反射机制在这个泛型为Integer的ArrayList中存放一个String类型的对象。*/
    @Test
    public void testAddStringToArrL() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        Class<? extends ArrayList> cls = list.getClass();
        Method add = cls.getDeclaredMethod("add", Object.class);
        add.invoke(list,"陶贤");
        System.out.println(list);   //[1, 我最帅]
    }
}
